package com.jyl.data.impl;

import com.jyl.data.List;

import java.util.function.Consumer;

/**
 * @ClassName LinkedList
 * @Author admin
 * @Date 2024/6/11
 */
public class LinkedList<E> implements List<E> {

    private static class Node<E> {
        private E element;
        private Node<E> next;

        Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }
    }

    private int size;

    private Node<E> head;

    private Node<E> tail;

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public void add(E element) {
        add(size, element);
    }

    /**
     * 获取 index 位置上的节点
     *
     * @param index
     * @return
     */
    private Node<E> getNode(int index) {
        Node<E> current = head;
        for (int i = 0; i < index; i++) {
            current = current.next;
        }
        return current;
    }

    /**
     * @param index   链表中的 index 节点
     * @param element 将要插入的新元素
     */
    @Override
    public void add(int index, E element) {
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException(index);
        }

        // 判断 head是否为空
        if (head == null) {
            head = new Node<>(element, null);
            tail = head;
        } else {
            if (index == 0) {
                head = new Node<>(element, head);
            } else if (index == size) { // 尾节点
                Node<E> node = new Node<>(element, null);
                tail.next = node;
                tail = node; // 更新尾指针的指向
            } else {
                Node<E> pre = getNode(index - 1);  // previous  目标节点的前一个节点
                pre.next = new Node<>(element, pre.next);
            }
        }
        size++;
    }

    @Override
    public E get(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException(index);
        }
        Node<E> node = getNode(index);
        return node.element;
    }

    /**
     * @param index 指定位置
     * @return
     */
    @Override
    public E remove(int index) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException(index);
        }
        E result;
        if (index == 0 && index == size - 1) {
            result = head.element;
            head.next = null;
            tail.next = null;
        } else if (index == 0) {
            result = head.element;
            head = head.next;
        } else if (index == size - 1) {
            result = tail.element;
            Node<E> pre = getNode(index - 1);
            pre.next = null;
            tail = pre;
        } else {
            Node<E> pre = getNode(index - 1);
            Node<E> current = pre.next;
            result = current.element;
            pre.next = current.next;
        }
        size--;
        return result;
    }

    @Override
    public E set(int index, E element) {
        if (index < 0 || index > size - 1) {
            throw new IndexOutOfBoundsException(index);
        }
        Node<E> node = getNode(index);
        E result = node.element;
        node.element = element;
        return result;
    }

    @Override
    public void forEach(Consumer<E> consumer) {
        Node<E> current = head;
        while (current != null) {
            consumer.accept(current.element);
            current = current.next;
        }
    }

    @Override
    public String toString() {

        if (size == 0) {
            return "[]";
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");

        forEach(e -> sb.append(e).append("->"));

        sb.append("null]");

        return sb.toString();
    }
}
